package com.xstudio.controllers;

import com.sun.javafx.scene.control.skin.VirtualFlow;
import com.xstudio.dao.*;
import com.xstudio.models.*;
import com.xstudio.mybatis.Pagination;
import com.xstudio.utilities.CommonUtility;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by kira on 16/2/28.
 */
@Controller
@RequestMapping(value = "/admin/users")
public class UserManageController {
    String prefix = "admin/users/";

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    SysUserMapperExtend sysUserMapperExtend;

    @RequestMapping(value = "/index")
    public String index(HttpServletRequest request, Model model) {
        Pagination pagination = CommonUtility.getPagination(request, model);
        HashMap<String, String> map = CommonUtility.getParameterMap(request);
        CommonUtility.renderGridData(request, model,
                sysUserMapperExtend.all(pagination, map),
                pagination);

        return prefix + "index";
    }

    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request, Model model) {
        return prefix + "add";
    }

    @RequestMapping(value = "/addEntity")
    @ResponseBody
    public String addEntity(HttpServletRequest request, Model model, SysUser sysUser) {
        sysUserMapper.insertSelective(sysUser);
        return "";
    }

    @RequestMapping(value = "/edit")
    public String edit(HttpServletRequest request, Model model, Integer id) {
        model.addAttribute("entity", sysUserMapper.selectByPrimaryKey(id));
        return prefix + "edit";
    }

    @RequestMapping(value = "/editEntity")
    @ResponseBody
    public String editEntity(HttpServletRequest request, Model model, SysUser sysUser) {
        sysUserMapper.updateByPrimaryKeySelective(sysUser);
        return "";
    }

    @RequestMapping(value = "/deleteEntity")
    @ResponseBody
    public String deleteEntity(HttpServletRequest request, Model model, Integer id) {
        sysUserMapper.deleteByPrimaryKey(id);
        return "";
    }

    @Resource
    SysRoleMapper sysRoleMapper;

    @Resource
    SysRoleUserMapper sysRoleUserMapper;

    @RequestMapping(value = "/configRole")
    public String configRole(HttpServletRequest request, Model model, Integer id) {
        model.addAttribute("list", sysRoleMapper.selectByExample(null));

        SysRoleUserExample example = new SysRoleUserExample();
        example.createCriteria().andUseridEqualTo(id);
        List<SysRoleUser> list = sysRoleUserMapper.selectByExample(example);

        List<Integer> roleids = new ArrayList<>();
        for (SysRoleUser sysRoleUser : list) {
            roleids.add(sysRoleUser.getRoleid());
        }

        List<SysRole> roleList = new VirtualFlow.ArrayLinkedList<>();
        if (roleids.size() > 0) {
            SysRoleExample sysRoleExample = new SysRoleExample();
            sysRoleExample.createCriteria().andIdIn(roleids);
            roleList = sysRoleMapper.selectByExample(sysRoleExample);
        }


        String roleidStr = "";
        for (SysRole sysRole : roleList) {
            roleidStr += sysRole.getId() + ",";
        }
        if (StringUtils.isNotEmpty(roleidStr)) {
            roleidStr = roleidStr.substring(0, roleidStr.length() - 1);
        }
        model.addAttribute("roleids", roleidStr);
        model.addAttribute("entity", sysRoleMapper.selectByPrimaryKey(id));


        return prefix + "configRole";
    }

    @RequestMapping(value = "/configRoleAction")
    @ResponseBody
    public String configRoleAction(HttpServletRequest request, Model model,
                                   Integer id, String roleids) {
        SysRoleUserExample example = new SysRoleUserExample();
        example.createCriteria().andUseridEqualTo(id);
        sysRoleUserMapper.deleteByExample(example);

        if (StringUtils.isNotEmpty(roleids)) {
            String[] roles = roleids.split(",");
            for (String role : roles) {
                SysRoleUser sysRoleUser = new SysRoleUser();
                sysRoleUser.setRoleid(Integer.parseInt(role));
                sysRoleUser.setUserid(id);
                sysRoleUserMapper.insertSelective(sysRoleUser);
            }
        }
        return "";
    }
}
